// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    goodsCart:[],
    allChecked:false,
    totalNum:0,
    totalPrice:0
  },
  onShow:function(){
    //获取购物地址
    const address = wx.getStorageSync('address');
     //获取缓存的购物信息
     const goodsCart = wx.getStorageSync('cart')||[];
     //是否全部选中
     const allChecked = goodsCart.length?goodsCart.every(v=>v.isActive):false;
    this.handlePriceNumber();
     this.setData({
      address,
      goodsCart,
      allChecked
    })
  },
  handleChooseAddress:function(e){
    let that = this;
    wx.getSetting({
     success:function(res){
        // console.log(res);
        const scopeAddress = res.authSetting["scope.address"];
        if(scopeAddress===true || scopeAddress===undefined){
          wx.chooseAddress({
            success:function(res){
                wx.setStorageSync('address', res);
                that.setData({
                  address:res
                })
            }
          })
        }else{
          wx.openSetting({
            success:function(res2){
              wx.chooseAddress({
                success:function(res3){
                  wx.setStorageSync('address', res);
                  that.setData({
                    address:res
                  })
                }
              })
            }
          })
        }
        
     }
    })
      
  },
  changeNum:function(e){
    const type = e.currentTarget.dataset.type;
    const goods_id = e.currentTarget.dataset.id;
    const goodsCart = this.data.goodsCart;
    const index = goodsCart.findIndex(v=>v.goods_id===goods_id)
    const that = this;
    // console.log(type);
    switch(type){
        case '+1':
          goodsCart[index].num++;
          break;
          case '-1':
            if(goodsCart[index].num===1){
              wx.showModal({
                title: '警告',
                content: '是否删除该商品？',
                success (res) {
                  if (res.confirm) {
                    goodsCart.splice(index,1);
                    wx.setStorageSync('cart', goodsCart);
                    that.setData({
                      goodsCart
                    })
                    that.handlePriceNumber();
                  } else if (res.cancel) {
                    goodsCart[index].num =1;
                  }
                }
              })
            }else{
              goodsCart[index].num--;
            }
          break;
    }
    // console.log(goodsCart);
    wx.setStorageSync('cart', goodsCart);
    this.setData({
      goodsCart
    })
    this.handlePriceNumber();
  },
  AllcheckChange:function(e){
    let allChecked = this.data.allChecked;
    //获取缓存的购物信息
    const goodsCart = wx.getStorageSync('cart')||[];
    if(goodsCart.length){
      goodsCart.map(v=>v.isActive=!allChecked)
      allChecked = !allChecked;
      wx.setStorageSync('cart', goodsCart)
      this.setData({
        goodsCart,
        allChecked
      })
      this.handlePriceNumber();
    }else{
      wx.showToast({
        title: '购物车空空如也',
        icon:"none"
      })
      this.setData({
        allChecked:false
      })
    }  
  },
  goodscheckChange:function(e){
      const goods_id = e.target.dataset.id;
      const goodsCart = this.data.goodsCart;
      const index = goodsCart.findIndex(v=>v.goods_id===goods_id)
      goodsCart[index].isActive = !goodsCart[index].isActive;
      wx.setStorageSync('cart', goodsCart)
      const allChecked = goodsCart.every(v=>v.isActive);
      this.setData({
        goodsCart,
        allChecked
      })
      this.handlePriceNumber();
  },
  handlePriceNumber:function(){
     //获取缓存的购物信息
     const goodsCart = wx.getStorageSync('cart')||[];
      //获取总价格 总数量
     let totalNum = 0;
     let totalPrice=0;
     goodsCart.forEach(function(v,index){
       if(v.isActive){
        totalNum +=v.num;
        totalPrice+=v.goods_price*v.num;
       }
     })
     this.setData({
      totalNum,
      totalPrice
    })
  },
  handleSettlement:function(e){
    //判断购物车列表勾选的商品数量和收货地址是否为空
    if(!this.data.address){
      wx.showToast({
        title: '请填写收货地址',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(!this.data.totalNum){
      wx.showToast({
        title: '你还没有选购商品',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
   
    wx.navigateTo({
      url: '/pages/pay/index'
    })
  }
})